﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Drawing.Printing;
using System.IO;
using System.Diagnostics;

namespace DevExpress.MailClient.Win.Controls.File_tabs
{

    /// <summary>
    /// http://www.codeproject.com/Articles/20823/Barcode-Image-Generation-Library
    /// </summary>

    public partial class ucFileBasicInformations : PRO_DOCS.BLL.WIN.Templates.DevxPermissionUserControlTemplate
    {

        protected void btnPrint_Click(object sender, EventArgs e)
        {

        }

        private void PrintPage(object o, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(picBarcodePrinting.Image, 0, 0);
        }
        string filePath;
        public void PRINT_BARCODE()
        {
            Bitmap createdImage = new Bitmap(bcCurrentFileBarcode.Width, bcCurrentFileBarcode.Height);

            bcCurrentFileBarcode.DrawToBitmap(createdImage, new Rectangle(0, 0, bcCurrentFileBarcode.Width, bcCurrentFileBarcode.Height));

            picBarcodePrinting.Image = createdImage;

            //filePath = Path.Combine(Application.UserAppDataPath, "0000000000");

            //new FileInfo(filePath).Delete();

            //createdImage.Save(filePath, System.Drawing.Imaging.ImageFormat.Jpeg);



            PrintDocument pd = new PrintDocument();
            pd.PrintPage += PrintPage;
            pd.Print();
            pd = null;

        }


        private void GET_LOCKED_VALUES()
        {
            PRO_DOCS.DAL.Database.proc_ARCHIVE_DEPARTMENT_FILE_LOCKED_STATUSLoadAllUNLOCKEDByFILE_IDResult CURRENT_LOCKED_FILE =
                        PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_LOCKED_STATUSLoadAllUNLOCKEDByFILE_ID(CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID).FirstOrDefault();

            if (CURRENT_LOCKED_FILE != null)
            {

                lblLockedDate.Visible = true;
                lblLockedBy.Visible = true;

                txtLockedBy.Visible = true;
                txtLockedDate.Visible = true;

                txtLockedBy.Text = CURRENT_LOCKED_FILE.EMPLOYEE_FULL_NAME;
                txtLockedDate.Text = CURRENT_LOCKED_FILE.ARCHIVE_DEPARTMENT_FILE_LOCKED_DATE.Value.ToString();

            }
            else
            {
                //lblLockedBy.Text = string.Empty;
                //lblLockedDate.Text = string.Empty;

                lblLockedDate.Visible = false;
                lblLockedBy.Visible = false;

                txtLockedBy.Visible = false;
                txtLockedDate.Visible = false;

                txtLockedBy.Text = string.Empty;
                txtLockedDate.Text = string.Empty;
            }
        }

        private PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE _cURRENT_ARCHIVE_DEPARTMENT_FILE;
        [Browsable(false)]
        public PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE CURRENT_ARCHIVE_DEPARTMENT_FILE
        {
            get { return _cURRENT_ARCHIVE_DEPARTMENT_FILE; }
            set
            {
                _cURRENT_ARCHIVE_DEPARTMENT_FILE = value;
                if (!_cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID.Equals(Guid.Empty))
                {
                    bcCurrentFileBarcode.Text = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_GENERATED_BARCODE_VALUE;
                    txtFileName.EditValue = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME;
                    txtFolderName.Text = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE.ARCHIVE_ARCHIVE_DEPARTMENTS_FOLDERS_STRUCTURE_NAME;
                    txtCreationDate.Text = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_CREATION_DATE.Value.ToString();
                    memoFileNotes.Text = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NOTES;
                    List<PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE_HISTORY> cURRENT_SELECTED_FILES_HISTORY = _cURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_HISTORies.ToList();
                    cURRENT_SELECTED_FILES_HISTORY.OrderBy(k => k.ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value);
                    txtCreatedBy.Text = cURRENT_SELECTED_FILES_HISTORY[0].LOGIN_USER.EMPLOYEE_FULL_NAME;

                    if (cURRENT_SELECTED_FILES_HISTORY.Count > 1)
                    {
                        txtLastModificationDate.Text = cURRENT_SELECTED_FILES_HISTORY[cURRENT_SELECTED_FILES_HISTORY.Count - 1].ARCHIVE_DEPARTMENT_FILE_HISTORY_UPLOADED_DATE.Value.ToString();
                        txtLastModifiedBy.Text = cURRENT_SELECTED_FILES_HISTORY[cURRENT_SELECTED_FILES_HISTORY.Count - 1].LOGIN_USER.EMPLOYEE_FULL_NAME;
                    }
                    else
                    {
                        txtLastModificationDate.Text = string.Empty;
                        txtLastModifiedBy.Text = string.Empty;
                    }

                    GET_LOCKED_VALUES();
                }
            }
        }
        public ucFileBasicInformations()
        {
            InitializeComponent();
            CURRENT_ARCHIVE_DEPARTMENT_FILE = new PRO_DOCS.DAL.Database.ARCHIVE_DEPARTMENT_FILE();
        }

        internal void LOCK_CURRENT_FILE()
        {
            PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_LOCK_FILE
                (CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID,
                PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID);
            GET_LOCKED_VALUES();
        }

        internal void UNLOCK_CURRENT_FILE()
        {
            PRO_DOCSDatabaseDataContext.proc_ARCHIVE_DEPARTMENT_FILE_UNLOCK_FILE
                (CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_ID,
                PRO_DOCS.BLL.WIN.Authentication.LOGGED_IN_EMPLOYEE_DETAILS.LOGIN_USER.LOGIN_USER_ID);
            GET_LOCKED_VALUES();
        }


        internal void SAVE_ARCHIVE_DEPARTMENT_FILES_BASIC_DATA(PRO_DOCS.DAL.Database.ARCHIVE_DOCUMENT_TYPE aRCHIVE_DOCUMENT_TYPE)
        {
            CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NAME = txtFileName.Text;
            CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_GENERATED_BARCODE_VALUE = bcCurrentFileBarcode.Text;
            CURRENT_ARCHIVE_DEPARTMENT_FILE.ARCHIVE_DEPARTMENT_FILE_DISPLAY_NOTES = memoFileNotes.Text;
        }
    }
}
